package cn.lixin.pet.controller;

import cn.lixin.basic.utils.AjaxResult;
import cn.lixin.basic.utils.LoginContext;
import cn.lixin.basic.utils.PageList;
import cn.lixin.pet.domain.Pet;
import cn.lixin.pet.query.PetQuery;
import cn.lixin.pet.service.IPetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping
    @ApiOperation(value = "高级查询",notes = "根据query高级查询")
    public PageList<Pet> pageList(@RequestBody PetQuery query, HttpServletRequest request){ // PathVariable：一个{}的时候可以不写
        return petService.queryPage(query);
    }

    @PostMapping("/onsale")
    @ApiOperation(value = "上架",notes = "上架")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "下架",notes = "下架")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Pet queryById(@PathVariable("id") Long id){
           return petService.queryById(id);
    }

    /**
     * 领养 ：
     * @param petId - petId
     * @param request 为了获取当前登录人 - 领养人
     * @return
     */
    @GetMapping("adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long petId,HttpServletRequest request){
        try {
            Long logininfoId = LoginContext.getCurrentLoginInfo(request).getId();
            petService.adopt(petId,logininfoId);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


}
